<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="举报类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="举报类型" clearable>
          <el-option
            v-for="dict in dict.type.community_report"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="举报理由" prop="reason">
        <el-input
          v-model="queryParams.reason"
          placeholder="请输入举报理由"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间筛序" prop="reason">
        <el-select
          v-model="queryParams.searchType"
          placeholder="请选择"
          clearable
        >
          <el-option label="默认" value="0" />
          <el-option label="最新" value="1" />
          <el-option label="最早" value="2" />
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['user:report:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['user:report:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['user:report:remove']"
          >删除</el-button>
        </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['user:report:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleRefreshCache"
          v-hasPermi="['user:report:remove']"
          >刷新缓存</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="configList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column
        label="用户名称"
        align="center"
        prop="userName"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="用户状态"
        align="center"
        prop="configKey"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column
        label="举报类型"
        align="center"
        prop="typeName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="举报分类"
        align="center"
        prop="reportContent"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="举报理由"
        align="center"
        prop="content"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.content || "-" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, true)"
            v-hasPermi="['user:report:edit']"
            v-if="scope.row.updateBy"
            >详情</el-button
          > -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, false)"
            v-hasPermi="['user:report:edit']"
            v-if="!scope.row.updateBy"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      append-to-body
    >
      <div class="reportBox">
        <div class="box">
          <div class="infoBox" v-if="rows.type != '3'">
            <img :src="rows.avatar" class="head" />
            <span class="name">{{ rows.userName }}</span>
            <span class="name">ID:{{ rows.userId }}</span>
          </div>

          <div class="articleBox" v-if="rows.type == '1'">
            <div class="title1">{{ articleInfo.title }}</div>
            <div v-html="articleInfo.content" />
            <el-image
              v-for="(item, index) in articleInfo.imageList"
              :key="index"
              class="uploadImg"
              :preview-src-list="rowsImg2"
              :initial-index="index"
              :src="item.imageUrl"
            />
            <vue-core-video-player
              v-if="articleInfo.videoList && articleInfo.videoList.length > 0"
              :src="articleInfo.videoList[0].imageUrl"
              :autoplay="false"
              style="width: 280px; height: 250px; margin: 0"
            />
          </div>
          <div class="" v-if="rows.type == '3'">
            <div class="replyBox">
              <img :src="commentInfo.avatar" class="head" />
              <div class="commentBox">
                <span class="gray">{{ commentInfo.nickName }}</span>
                <div v-html="commentInfo.content" />
                <el-image
                  :key="index"
                  v-if="commentInfo.imageUrl"
                  class="uploadImg"
                  :preview-src-list="[commentInfo.imageUrl]"
                  :initial-index="index"
                  :src="commentInfo.imageUrl"
                />

                <div class="gray" style="display: flex; align-items: baseline">
                  {{
                    commentInfo.createTime &&
                    commentInfo.createTime.slice(0, 10)
                  }}
                  <div>{{ commentInfo.address }}</div>
                </div>
                <div v-if="expressReply">
                  <div
                    :key="index"
                    class="replyBox"
                    style="margin-top: 10px"
                    v-for="(item, index) in commentInfo.firstReply"
                  >
                    <img :src="item.avatar" class="head" />
                    <div class="commentBox">
                      <span class="gray">{{ item.nickName }}</span>
                      <div v-html="item.content" />
                      <el-image
                        :key="index"
                        v-if="item.imageUrl"
                        class="uploadImg"
                        :preview-src-list="[item.imageUrl]"
                        :initial-index="index"
                        :src="item.imageUrl"
                      />
                      <div
                        class="gray"
                        style="display: flex; align-items: baseline"
                      >
                        {{ item.createTime && item.createTime.slice(0, 10) }}
                        <div>{{ item.address }}</div>
                      </div>
                    </div>
                  </div>
                </div>

                <div
                  @click="expressReply = !expressReply"
                  v-if="!expressReply"
                  class="gray"
                  style="display: flex; align-items: center"
                >
                  <div>——展开更多回复</div>
                  <u-icon class="el-icon-arrow-down"></u-icon>
                </div>
                <div
                  v-if="expressReply"
                  @click="expressReply = !expressReply"
                  class="gray"
                  style="display: flex; align-items: center"
                >
                  <div>——收起更多回复</div>
                  <u-icon class="el-icon-arrow-up"></u-icon>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="box">
          <div class="contentBox">
            <span class="label">举报类型：</span>
            <span>{{ rows.typeName }}</span>
          </div>
          <div class="contentBox">
            <span class="label">举报分类：</span>
            <span>{{ rows.reportContent }}</span>
          </div>
          <div class="contentBox" v-if="rows.type != '3'">
            <span class="label">举报描述：</span>
            <span>{{ rows.content }}</span>
          </div>
          <div class="contentBox" v-if="rows.type != '3'">
            <span class="label">举报图片：</span>
          </div>
          <div style="margin-top: 15px" v-if="rows.type != '3'">
            <el-image
              v-for="(item, index) in rows.imageList"
              :key="index"
              class="uploadImg"
              :preview-src-list="rowsImg"
              :initial-index="index"
              :src="item.imageUrl"
            />
          </div>
        </div>
        <div class="box">
          <el-form
            :model="form"
            ref="editForm"
            size="small"
            label-width="100px"
          >
            <!-- <template v-if="rows.type == '3'">
              <el-col>
                <el-form-item label="举报评论">
                  <editor
                    :value="
                      rows.replyId
                        ? commentInfo.firstReply[0].content
                        : commentInfo.content
                    "
                    :min-height="150"
                    readOnly
                  ></editor>
                </el-form-item>
              </el-col>
            </template> -->
            <el-col>
              <el-form-item label="处置类型" prop="disposalType">
                <el-select
                  v-model="form.disposalType"
                  placeholder="处置类型"
                  clearable
                >
                  <el-option
                    v-for="dict in getDisposalType(rows.type)"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col v-if="form.disposalType == '1' || form.disposalType == '2'">
              <el-form-item label="处置时间" prop="disposalTime">
                <el-select
                  v-model="form.disposalTime"
                  placeholder="选择处置时间"
                  clearable
                >
                  <el-option
                    v-for="dict in dict.type.community_report_time"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="处罚原因">
                <el-input
                  v-model="form.dispositionReason"
                  placeholder="请输入处罚原因"
                />
              </el-form-item>
            </el-col>
          </el-form>
        </div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button v-if="!isDetail" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listConfig,
  getConfig,
  delConfig,
  addConfig,
  updateConfig,
  refreshCache,
} from "@/api/system/config";
import {
  getReportLit,
  reportPunish,
  reportList,
  getReply,
  getCommentReply,
} from "@/api/monitor/report";
export default {
  name: "Config",
  dicts: [
    "sys_yes_no",
    "community_report",
    "community_report_type",
    "community_report_time",
    "community_article_type",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        configName: undefined,
        configKey: undefined,
        configType: undefined,
        searchType: "1",
      },
      // 表单参数
      form: {},
      rows: {},
      // 表单校验
      rules: {
        configName: [
          { required: true, message: "参数名称不能为空", trigger: "blur" },
        ],
        configKey: [
          { required: true, message: "参数键名不能为空", trigger: "blur" },
        ],
        configValue: [
          { required: true, message: "参数键值不能为空", trigger: "blur" },
        ],
      },
      reportType: [],
      isDetail: false,
      articleInfo: {},
      commentInfo: {},
      rowsImg: [],
      expressReply: false,
    };
  },
  created() {
    this.getList();
    this.getType();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      getReportLit(this.queryParams, this.dateRange).then((response) => {
        this.configList = response.data;
        console.log(this.configList);
        this.total = response.total;
        this.loading = false;
      });
    },
    getArticle(id) {
      reportList({
        pageNum: 1,
        pageSize: 10,
        id: id,
      }).then((res) => {
        this.articleInfo = res.rows[0];
        this.rowsImg2 = [];
        this.articleInfo.imageList.forEach((item) => {
          this.rowsImg2.push(item.imageUrl);
        });
        console.log(this.articleInfo);
      });
    },
    getImageList(res) {
      let imgList = [];
      res.forEach((item) => {
        imgList.push(item.imageUrl);
      });
      return imgList;
    },
    getDisposalType(type) {
      console.log(this.dict.type.community_report_type);
      let report_type = this.dict.type.community_report_type;
      if (type == "1") {
        let result = [];
        for (let i = 0; i < report_type.length; i++) {
          if (report_type[i].label != "删除") {
            result.push(report_type[i]);
          }
        }
        return result;
      } else if (type == "2") {
        return this.dict.type.community_report_type;
      } else {
        let result = [];
        for (let i = 0; i < report_type.length; i++) {
          if (report_type[i].label != "静置") {
            result.push(report_type[i]);
          }
        }
        return result;
      }
    },
    getComment(row) {
      getCommentReply({
        articleId: row.articleId,
        commentId: row.commentId,
        currentPage: 1,
        limit: 100,
      }).then((res) => {
        console.log(res);
        if (res.data && res.data.length > 0) {
          this.commentInfo = res.data[0];
        }
      });
    },
    getType() {
      this.getDicts("community_report").then((response) => {
        console.log(response.data);
        this.reportType = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参数";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.configId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row, detail) {
      this.isDetail = detail;
      this.reset();

      this.rows = row;
      this.rowsImg = [];
      if (row.imageList.length > 0) {
        row.imageList.forEach((item) => {
          this.rowsImg.push(item.imageUrl);
        });
      }
      console.log("handleUpdate", row);
      this.open = true;
      this.title = "审核举报";
      if (row.type == "1") {
        this.getArticle(row.articleId);
      } else if (row.type == "3") {
        this.getComment(row);
      }
      
      // 调用获取列表接口，传入对应的id参数
      // const params = {
      //   ...this.queryParams,
      //   id: row.id
      // };
      // getReportLit(params, this.dateRange).then((response) => {
      //   console.log("获取列表数据:", response);
      //   // 可以根据需要更新当前行的数据
      //   const updatedRow = response.data.find(item => item.id === row.id);
      //   if (updatedRow) {
      //     Object.assign(row, updatedRow);
      //   }
      // });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          console.log(this.form, this.rows, "====");
          
          // 构建符合接口要求的参数
          let params = {
            id: this.rows.id, // 必需
            type: this.rows.type, // 举报类型：1:文章举报 2:个人举报 3:评价举报
            typeId: this.rows.type === '1' ? this.rows.articleId : (this.rows.type === '3' ? this.rows.commentId : ''), // 文章/评论ID，可选但文章或评论举报必填
            userId: this.rows.userId, // 被举报人ID，必需
            disposalType: this.form.disposalType, // 处罚类型：0：不做处理,1:封号,2:禁言,3:静置,4:删除
            disposalTime: this.form.disposalTime, // 处罚时间，必需
            dispositionReason: this.form.dispositionReason, // 处罚原因，必需
            reportContent: this.rows.reportContent, // 举报理由，必需
            content: this.rows.content || '', // 举报描述，必需
            images: this.rows.imageList ? this.rows.imageList.map(item => item.imageUrl) : [], // 举报图片数组
            isReply: this.rows.type === '3' // 是否回复，评价举报时为true
          };
          
          reportPunish(params).then((response) => {
            this.$modal.msgSuccess("审核成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configIds = row.configId || this.ids;
      this.$modal
        .confirm('是否确认删除参数编号为"' + configIds + '"的数据项？')
        .then(function () {
          return delConfig(configIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/config/export",
        {
          ...this.queryParams,
        },
        `config_${new Date().getTime()}.xlsx`
      );
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
      });
    },
  },
};
</script>
<style scoped lang="scss">
.reportBox {
  width: 100%;
  border: 1px solid #f5f5f5;
  display: flex;
  padding: 50px;
}
.box {
  display: flex;
  flex-direction: column;
  width: 30%;
}
.contentBox {
  display: flex;
  padding-bottom: 15px;
  .label {
    font-weight: bold;
  }
}
.infoBox {
  display: flex;
  align-items: center;
  .name {
    margin-left: 10px;
  }
}

.replyBox {
  display: flex;
  .name {
    margin-left: 10px;
  }
}
.commentBox {
  display: flex;
  flex-direction: column;
  margin: 10px;
}
.articleBox {
  margin-top: 20px;
  .title1 {
    font-weight: bold;
  }
}

.head {
  width: 30px;
  height: 30px;
  border-radius: 50%;
}

.uploadImg {
  width: 150px;
  height: 150px;
}
.gray {
  color: #aaa;
}
</style>
